package com.terrorism.sdk.utils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.ImageView;

import com.terrorism.sdk.R;
import com.terrorism.sdk.view.CustomToast;

/**
 * @since Monday, June 02, 2014
 * @author hoangcaomobile
 *
 */
public class PhotoUtils {

	// process existed photo and return photo path
	public static String getPathPhotoProcessed(Activity activity, String folderParent, String saveToFolderName, String originPhotoPath) {
		String newPhotoPath = null;
		if (FileUtils.isSDCardOK(folderParent + "/" + saveToFolderName)) {
			if (FileUtils.getFileType(originPhotoPath).equals("gif") || FileUtils.getFileType(originPhotoPath).endsWith("GIF")) {
				newPhotoPath = originPhotoPath;
			} else {
				File fileNew = new File(FileUtils.folderSystem + "/" + folderParent + "/" + saveToFolderName + 
						"/Photo_" + System.currentTimeMillis() + ".jpg");
				FileOutputStream fos;
				try {
					fos = new FileOutputStream(fileNew);					
					BitmapFactory.Options options = new BitmapFactory.Options();

					// resize
					Double fileSize = FileUtils.getFileSize(originPhotoPath);
					if (fileSize >= 8000) {
						options.inSampleSize = 8;
					} else if (fileSize < 8000 && fileSize >= 6000) {
						options.inSampleSize = 6;
					} else if (fileSize < 6000 && fileSize >= 1000) {
						options.inSampleSize = 4; // 1/2 of original photo
					} else if (fileSize < 1000 && fileSize >= 500) {
						options.inSampleSize = 2; // 1/2 of original photo
					} else {
						options.inSampleSize = 1;
					}
					// --

					BitmapFactory.decodeFile(originPhotoPath, options).compress(Bitmap.CompressFormat.JPEG, 99, fos);
					fos.close();
					newPhotoPath =  fileNew + "";

					Log.i("PhotoProcess", "Origin file size: " + Double.toString(fileSize) + " KB"
							+ " | Match resize: " + options.inSampleSize
							+ " | New file size: " + Double.toString(FileUtils.getFileSize(newPhotoPath)) + " KB");
				} catch (FileNotFoundException e) {
				} catch (IOException e) {
				}
			}
		} else {
			CustomToast.vToastLong(activity, activity.getString(R.string.toastErrorSDCard));
		}
		return newPhotoPath;
	}
	// --

	// process bitmap and return photo path with a folder
	public static String getPathBitmapProcessed(Activity activity, String folderName, Bitmap bitmap) {
		String newPhotoPath = null;
		if (FileUtils.isSDCardOK(folderName)) {
			File fileNew = new File(FileUtils.folderSystem + "/" + folderName + 
					"/Photo_" + System.currentTimeMillis() + ".jpg");
			FileOutputStream fos;
			try {
				fos = new FileOutputStream(fileNew);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
				fos.close();
				newPhotoPath =  fileNew + "";
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			}
		} else {
			CustomToast.vToastLong(activity, activity.getString(R.string.toastErrorSDCard));
		}
		return newPhotoPath;
	}
	// --

	// process bitmap and return photo path with 2 folder
	public static String getPathBitmapProcessed2(Activity activity, String folderParent, String saveToFolderName, Bitmap bitmap) {
		String newPhotoPath = null;
		if (FileUtils.isSDCardOK(folderParent + "/" + saveToFolderName)) {
			File fileNew = new File(FileUtils.folderSystem + "/" + folderParent + "/" + saveToFolderName + 
					"/Photo_" + System.currentTimeMillis() + ".jpg");
			FileOutputStream fos;
			try {
				fos = new FileOutputStream(fileNew);
				bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
				fos.close();
				newPhotoPath =  fileNew + "";
			} catch (FileNotFoundException e) {
			} catch (IOException e) {
			}
		} else {
			CustomToast.vToastLong(activity, activity.getString(R.string.toastErrorSDCard));
		}
		return newPhotoPath;
	}
	// --

	// get bitmap thumbnail via screen width
	public static Bitmap getThumbnailSquare(Activity activity, Bitmap bitmapOrigin, int divideScreenWidth) {
		Bitmap newBitmap = ThumbnailUtils.extractThumbnail(bitmapOrigin,
				(ScreenUtils.getScreenWidth(activity) / divideScreenWidth),
				(ScreenUtils.getScreenWidth(activity) / divideScreenWidth));
		return newBitmap;
	}
	// --

	// get bitmap circle via screen width
	public static Bitmap getThumbnailCircle(Activity activity, Bitmap bitmapOrigin, int divideScreenWidth) {
		Bitmap bitmap = ThumbnailUtils.extractThumbnail(bitmapOrigin,
				(ScreenUtils.getScreenWidth(activity) / divideScreenWidth),
				(ScreenUtils.getScreenWidth(activity) / divideScreenWidth));
		Bitmap output;

		if (bitmap.getWidth() > bitmap.getHeight()) {
			output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
		} else {
			output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Config.ARGB_8888);
		}

		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

		float r = 0;

		if (bitmap.getWidth() > bitmap.getHeight()) {
			r = bitmap.getHeight() / 2;
		} else {
			r = bitmap.getWidth() / 2;
		}

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawCircle(r, r, r, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}
	// --

	// get bitmap circle input width and height
	public static Bitmap getThumbnailCircleWidthHeight(Bitmap bitmapOrigin, int width, int height) {
		Bitmap bitmap = ThumbnailUtils.extractThumbnail(bitmapOrigin, width, height);
		Bitmap output;

		if (bitmap.getWidth() > bitmap.getHeight()) {
			output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
		} else {
			output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), Config.ARGB_8888);
		}

		Canvas canvas = new Canvas(output);

		final int color = 0xff424242;
		final Paint paint = new Paint();
		final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());

		float r = 0;

		if (bitmap.getWidth() > bitmap.getHeight()) {
			r = bitmap.getHeight() / 2;
		} else {
			r = bitmap.getWidth() / 2;
		}

		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(color);
		canvas.drawCircle(r, r, r, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}
	// --

	public static Bitmap getOriginBitmapFromImageView(ImageView imageView) {
		return ((BitmapDrawable)imageView.getDrawable()).getBitmap();
	}

	// get real photo path from Uri
	public static String getRealPhotoPathFromURI(Context context, Uri contentURI) {
		Cursor cursor = context.getContentResolver()
				.query(contentURI, null, null, null, null); 
		cursor.moveToFirst(); 
		int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
		System.out.println("idx = "+idx);
		if (idx == -1){return new String("null");}

		return cursor.getString(idx); 
	}
	// --


	public static void vUpdatePhotoToGallery(Activity mActivity, String photoPath) {
		try {
			Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
			Uri contentUri = Uri.fromFile(new File(photoPath));
			mediaScanIntent.setData(contentUri);
			mActivity.sendBroadcast(mediaScanIntent);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
